package com.shigui.news.dao.impl;

import java.util.List;


import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.shigui.news.dao.BaseDao;
import com.shigui.news.dao.CommentDao;
import com.shigui.news.model.Comment;
import com.shigui.news.model.News;
/**
 * 
 * @author Shiguiwu
 * 评论
 *dao层，负责连接数据库
 */
@Repository("commentDao")
public class CommentDaoImpl extends BaseDao implements CommentDao {
	@Autowired
	private SessionFactory sessionFactory;
	
	
	/**
	 * 根据标号查询评论
	 */
	@Override
	public Comment selectById(Integer id) {

		Query query = getSession().createQuery("from Comment where id=:id");
		
		query.setInteger("id", id);
		
		Comment comment = (Comment)query.uniqueResult();
		
		return comment;
	}

	@Override
	public List<Comment> select() {

		return null;
	}
	/**
	 * 插入评论，必须加事务
	 */
	@Transactional
	@Override
	public void insert(Comment comment) {

		getSession().save(comment);
	}
	/**
	 * 修改评论
	 */
	
	@Transactional
	@Override
	public void update(Comment comment) {

		getSession().update(comment);
	}
	/**
	 * 删除评论
	 */
	@Transactional
	@Override
	public void delete(Comment comment) {

		getSession().delete(comment);
	}

	@Override
	public List<Comment> getCommentList(Integer newsId) {
//
//		Query query = getSession().createQuery("select from Comment  where newsId=:newsId");
//		
//		query.setInteger("newsId", newsId);//.getNewsId() );
		
		List<Comment> commentsList = (List<Comment>)getSession().createCriteria(Comment.class)
							.add(Restrictions.eq("newsId", newsId))
							.list();
		
		
		return commentsList;
	}
	@Transactional
	@Override
	public void delete(Integer newsId) {

		Query query = getSession().createQuery("delete from Comment where newsId =:newsId");
		
		query.setInteger("newsId", newsId );
	}
	@Transactional
	@Override
	public void delete(News news) {
		// TODO Auto-generated method stub
		Query query = getSession().createQuery("delete from Comment where newsId =:newsId");
		query.setInteger("newsId", news.getNewsId() );
	}
	/**
	 * page:第几页页数
	 * count:每页的条数
	 * firstResult:从第几页开始
	 * maxResults:每页的条数
	 * 比如我有100条数据，page=5,count=10,现在我要去第五页的记录
	 * 则从第51条开始 ：（5-1）*10=40
	 */
	@Override
	public List<Comment> select(News news, Integer page, Integer count) {

		Integer firstResult = (page-1) * count;
		
		List<Comment> commentsList = (List<Comment>)getSession().createCriteria(Comment.class)
				.add(Restrictions.eq("newsId", news.getNewsId()))
				.setFirstResult(firstResult)
				.setMaxResults(count)
				.list();


			return commentsList;
	}

	@Override
	public Integer countCommentsList(News news) {
		Criteria criteria = getSession().createCriteria(Comment.class)
				.add(Restrictions.eq("newsId", news.getNewsId()))
				.setProjection(Projections.rowCount());
		
		Integer total = Integer.valueOf(criteria.uniqueResult().toString());
		
		return total;
	}

}
